//
//  ZJMapControlView.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/6/12.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJMapControlView.h"

#define TEXT_COLOR @"a8a8a8"//党组织未选中的字体颜色
#define TEXT_FONT  [UIFont systemFontOfSize: 10 *  [UIScreen zj_textFontScale]]//党组织等字体大小
@interface ZJMapControlView ()
{
    UIButton *_seleButton;/** 党组织、活动、阵地的选中按钮 */
    UIButton *_seleButton1;/** 当前位置、在职坐标点、在职坐标点 */
}
    
@end

@implementation ZJMapControlView

- (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.userInteractionEnabled = NO;
            [self addSubview:self.meunButton];
            [self addSubview:self.currentLocation];
            [self addSubview:self.workAddress];
            [self addSubview:self.homeButton];
            [self addSubview:self.organizationButton];
            [self addSubview:self.activityButton];
            [self addSubview:self.positionButton];
            
            self.organizationButton.selected = YES;
            _seleButton = self.organizationButton;
            self.organizationButton.backgroundColor = [UIColor themeColor];
        }
        return self;
    }
    
    

    
#pragma mark - 点击事件
///菜单
- (void)action_meunButton:(UIButton *)button{
    
    if ([self.delegate respondsToSelector:@selector(ZJMapControlViewDelegate_meunButtonClick)]) {
        [self.delegate ZJMapControlViewDelegate_meunButtonClick];
    }
}
///当前位置
- (void)action_currentLocation:(UIButton *)button{
    [self seleButtonChange_button1:button];
    if ([self.delegate respondsToSelector:@selector(ZJMapControlViewDelegate_currentLocationButtonClick)]) {
        [self.delegate ZJMapControlViewDelegate_currentLocationButtonClick];
    }
}
///home
- (void)action_homeButton:(UIButton *)button{
    [self seleButtonChange_button1:button];
    if ([self.delegate respondsToSelector:@selector(ZJMapControlViewDelegate_homeButtonClick)]) {
        [self.delegate ZJMapControlViewDelegate_homeButtonClick];
    }
}
///办公地址
- (void)action_workAddressButton:(UIButton *)button{
    [self seleButtonChange_button1:button];
    if ([self.delegate respondsToSelector:@selector(ZJMapControlViewDelegate_workAddressButton)]) {
        [self.delegate ZJMapControlViewDelegate_workAddressButton];
    }
}

///活动
- (void)action_organizationButton:(UIButton *)button{
    [self seleButtonChange_button:button];
    if ([self.delegate respondsToSelector:@selector(ZJMapControlViewDelegate_organizationButtonClick)]) {
        [self.delegate ZJMapControlViewDelegate_organizationButtonClick];
    }
    
}
///需求
- (void)action_positionButton:(UIButton *)button{
    [self seleButtonChange_button:button];
    if ([self.delegate respondsToSelector:@selector(ZJMapControlViewDelegate_positionButtonClick)]) {
        [self.delegate ZJMapControlViewDelegate_positionButtonClick];
    }
}
///项目
- (void)action_activityButton:(UIButton *)button{
    [self seleButtonChange_button:button];
    if ([self.delegate respondsToSelector:@selector(ZJMapControlViewDelegate_activityButtonClick)]) {
        [self.delegate ZJMapControlViewDelegate_activityButtonClick];
    }
}

- (void)seleButtonChange_button:(UIButton  *)button{
    _seleButton.selected = NO;
    _seleButton.backgroundColor = [UIColor whiteColor];
    button.selected =YES;
    button.backgroundColor = [UIColor themeColor];
    _seleButton = button;
    
    
}

- (void)seleButtonChange_button1:(UIButton  *)button{
    _seleButton1.selected = NO;
    _seleButton1.backgroundColor = [UIColor whiteColor];
    button.selected =YES;
    button.backgroundColor = [UIColor themeColor];
    _seleButton1 = button;
}


- (void)chooseFromString:(NSString *)string clickIndex:(NSInteger)index{
    if ([string isEqualToString:@"act"]) {
        switch (index) {
            case 0:{
                [self seleButtonChange_button:self.organizationButton];
                
            }
                break;
            case 1:{
                [self seleButtonChange_button:self.activityButton];
            }
                break;
            case 2:{
                [self seleButtonChange_button:self.positionButton];
            }
                
                break;
            default:
                break;
        }
    }else{
        switch (index) {
            case 0:{
                [self seleButtonChange_button:self.organizationButton];
               
            }
                break;
            case 1:{
                
                [self seleButtonChange_button:self.activityButton];
                
            }
                 break;
            case 2:{
                
                [self seleButtonChange_button:self.positionButton];
                
            }
                break;
            default:
                break;
        }

    
    }

}
///判断手势
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
        if (CGRectContainsPoint(self.meunButton.frame, point)) {
            return self.meunButton;
        }else if (CGRectContainsPoint(self.homeButton.frame, point)){
            if ([[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.AUTH_STATUS isEqualToString:@"1"]) {//审核中
                return nil;
            }else{
                return self.homeButton;
            }
            
        
        }else if (CGRectContainsPoint(self.currentLocation.frame, point)){
            return self.currentLocation;
            
        }else if (CGRectContainsPoint(self.organizationButton.frame, point)){
            return self.organizationButton;
            
        }else if (CGRectContainsPoint(self.activityButton.frame, point)){
            return self.activityButton;
            
        }else if (CGRectContainsPoint(self.positionButton.frame, point)){
            
            return self.positionButton;
        }else if (CGRectContainsPoint(self.workAddress.frame, point)){
            
            return self.workAddress;
        }
        
        return nil;
}
 
    
    
#pragma mark - Setter
    
    
#pragma mark - Getters
- (UIButton *)meunButton{
    if (_meunButton == nil){
        _meunButton = [[UIButton alloc]initWithFrame:CGRectMake(self.width - 65 * [UIScreen zj_screenWScale], self.height - 70 * [UIScreen zj_screenHScale], 50 * [UIScreen zj_screenHScale], 50 * [UIScreen zj_screenHScale])];
        _meunButton.backgroundColor = [UIColor whiteColor];
        _meunButton.layer.cornerRadius = _meunButton.width / 2;
        _meunButton.layer.borderWidth = 1;
        _meunButton.layer.borderColor = [UIColor zj_colorWithHexString:@"d2d2ca"].CGColor;
        [_meunButton setImage:[UIImage imageNamed:@"btn_caidan"] forState:UIControlStateNormal];
        [_meunButton addTarget:self action:@selector(action_meunButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _meunButton;
}
    
- (UIButton *)currentLocation{
    if (_currentLocation == nil){
        _currentLocation = [[UIButton alloc]initWithFrame:CGRectMake(16 * [UIScreen zj_screenWScale],self.height - 16 * [UIScreen zj_screenHScale] - self.meunButton.height, self.meunButton.width, self.meunButton.height)];
        [_currentLocation setImage:[UIImage imageNamed:@"icon_com_suoding"] forState:UIControlStateNormal];
         [_currentLocation setImage:[UIImage imageNamed:@"icon_com_suoding1"] forState:UIControlStateSelected];
        _currentLocation.backgroundColor = [UIColor whiteColor];
        _currentLocation.layer.cornerRadius = self.meunButton.layer.cornerRadius;
        _currentLocation.layer.borderWidth = 1;
        _currentLocation.layer.borderColor = [UIColor zj_colorWithHexString:@"d2d2ca"].CGColor;
        [_currentLocation addTarget:self action:@selector(action_currentLocation:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _currentLocation;
}
- (UIButton *)homeButton{
    if (_homeButton == nil){
        _homeButton = [[UIButton alloc]initWithFrame:CGRectMake(self.currentLocation.x,self.currentLocation.y - 8 * [UIScreen zj_screenHScale] - self.meunButton.height, self.meunButton.width, self.meunButton.height)];
        [_homeButton setImage:[UIImage imageNamed:@"icon_com_home"] forState:UIControlStateNormal];
        [_homeButton setImage:[UIImage imageNamed:@"icon_com_home1"] forState:UIControlStateSelected];
        _homeButton.backgroundColor = [UIColor whiteColor];
        _homeButton.layer.cornerRadius = self.meunButton.layer.cornerRadius;
        _homeButton.layer.borderWidth = 1;
        _homeButton.layer.borderColor = [UIColor zj_colorWithHexString:@"d2d2ca"].CGColor;
        [_homeButton addTarget:self action:@selector(action_homeButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _homeButton;
}

- (UIButton *)workAddress{
    if (_workAddress == nil){
        _workAddress = [[UIButton alloc]initWithFrame:CGRectMake(self.currentLocation.x,self.homeButton.y - 8 * [UIScreen zj_screenHScale] - self.meunButton.height, self.meunButton.width, self.meunButton.height)];
        [_workAddress setImage:[UIImage imageNamed:@"map_cot_company"] forState:UIControlStateNormal];
        [_workAddress setImage:[UIImage imageNamed:@"map_cot_company1"] forState:UIControlStateSelected];
        _workAddress.backgroundColor = [UIColor whiteColor];
        _workAddress.layer.cornerRadius = self.meunButton.layer.cornerRadius;
        _workAddress.layer.borderWidth = 1;
        _workAddress.layer.borderColor = [UIColor zj_colorWithHexString:@"d2d2ca"].CGColor;
        [_workAddress addTarget:self action:@selector(action_workAddressButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _workAddress;
}
    
- (UIButton *)organizationButton{
    if (_organizationButton == nil){
        _organizationButton = [[UIButton alloc] initWithFrame:CGRectMake(self.width - self.meunButton.width - 10 * [UIScreen zj_screenWScale], 27 * [UIScreen zj_screenWScale], self.meunButton.width, self.meunButton.height)];
        [self setupButton:_organizationButton title:@"党组织" normalImageName:@"dangzuzhi1" selectImageName:@"dangzuzhi2"];
        [_organizationButton addTarget:self action:@selector(action_organizationButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _organizationButton;
}
- (UIButton *)activityButton{
    if (_activityButton == nil){
        _activityButton = [[UIButton alloc] initWithFrame:CGRectMake(self.organizationButton.x, self.organizationButton.bottom + 11 * [UIScreen zj_screenHScale], self.meunButton.width, self.meunButton.height)];
        [self setupButton:_activityButton title:@"活动" normalImageName:@"huodong1" selectImageName:@"huodong2"];
        [_activityButton addTarget:self action:@selector(action_activityButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _activityButton;
}
- (UIButton *)positionButton{
    if (_positionButton == nil){
        _positionButton = [[UIButton alloc] initWithFrame:CGRectMake(self.organizationButton.x, self.activityButton.bottom + 11 * [UIScreen zj_screenHScale], self.meunButton.width, self.meunButton.height)];
        [self setupButton:_positionButton title:@"阵地" normalImageName:@"zhendi1" selectImageName:@"zhendi2"];
        [_positionButton addTarget:self action:@selector(action_positionButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _positionButton;
}
- (void)setUpHomeAndWorkAddressLocation{
    if ([[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.AUTH_STATUS isEqualToString:@"1"]) {//审核中
        self.homeButton.hidden = YES;
        CGRect wordAddress = self.workAddress.frame;
        wordAddress.origin.y = self.currentLocation.y - 8 * [UIScreen zj_screenHScale] - self.meunButton.height;
        self.workAddress.frame = wordAddress;
        
        
        CGRect homeAddress = self.homeButton.frame;
        homeAddress.origin.y = self.workAddress.y - 8 * [UIScreen zj_screenHScale] - self.meunButton.height;
        self.homeButton.frame = homeAddress;

    }else{
        self.homeButton.hidden = NO;
        CGRect homeAddress = self.homeButton.frame;
        homeAddress.origin.y = self.currentLocation.y - 8 * [UIScreen zj_screenHScale] - self.meunButton.height;
        self.homeButton.frame = homeAddress;
        
        CGRect wordAddress = self.workAddress.frame;
        wordAddress.origin.y = self.homeButton.y - 8 * [UIScreen zj_screenHScale] - self.meunButton.height;
        self.workAddress.frame = wordAddress;

    
    }

}

- (void)setupButton:(UIButton *)button title:(NSString *)title normalImageName:(NSString *)normalImageName selectImageName:(NSString *)selectImageName{
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor zj_colorWithHexString:TEXT_COLOR] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [button setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selectImageName] forState:UIControlStateSelected];
    button.titleLabel.font = TEXT_FONT;
    
    button.titleEdgeInsets = UIEdgeInsetsMake(22 * [UIScreen zj_screenHScale], - button.imageView.size.width, 0.0, 0.0);
    button.imageEdgeInsets = UIEdgeInsetsMake(- 11 * [UIScreen zj_screenHScale], (button.bounds.size.width - button.imageView.size.width)/2, 0.0,(button.bounds.size.width - button.imageView.size.width)/2);
    button.layer.cornerRadius = 3 * [UIScreen zj_screenWScale];
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor zj_colorWithHexString:@"d2d2ca"].CGColor;
}



@end
